package com.wbdgj.ui.booking;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.internal.LinkedTreeMap;
import com.igexin.sdk.PushManager;
import com.wbdgj.R;
import com.wbdgj.adapter.YhqMyAdapter;
import com.wbdgj.base.BaseApplication;
import com.wbdgj.base.BaseFragment;
import com.wbdgj.http.HttpAdapter;
import com.wbdgj.http.OnNoResponseListener;
import com.wbdgj.http.OnResponseListener;
import com.wbdgj.model.ResultObjectModel;
import com.wbdgj.ui.home.BranchCouponActivity;
import com.wbdgj.ui.home.NewsAllActivity;
import com.wbdgj.ui.login.LoginActivity;
import com.wbdgj.ui.mine.InformaticaNewActivity;
import com.wbdgj.ui.mine.SettingActivity;
import com.wbdgj.ui.mine.YjfkActivity;
import com.wbdgj.ui.mine.YqhyActivity;
import com.wbdgj.ui.store.PointsDetailsActivity;
import com.wbdgj.ui.store.PointsMallActivity;
import com.wbdgj.utils.SpKeyUtils;
import com.wbdgj.utils.ToastUtils;
import com.wbdgj.views.AllRoundImageView;

import java.text.DecimalFormat;
import java.util.ArrayList;

import butterknife.BindView;
import in.srain.cube.views.ptr.PtrClassicFrameLayout;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.PtrHandler;
import retrofit2.Call;

public class BookingMineFragment extends BaseFragment implements PtrHandler {

    private Context context;

    @BindView(R.id.pfl_root)
    PtrClassicFrameLayout pfl_root;

    @BindView(R.id.roundImg)
    AllRoundImageView roundImg;

    @BindView(R.id.name)
    TextView name;
    @BindView(R.id.card)
    TextView card;

    @BindView(R.id.account_balance)
    TextView account_balance;
    @BindView(R.id.yhqNum)
    TextView yhqNum;
    @BindView(R.id.jfTxt)
    TextView jfTxt;

    private String ID_CARD;

    @Override
    protected int getLayout() {
        return R.layout.ui_booking_mine;
    }

    @Override
    protected void onInit() {
        context = getActivity();
        pfl_root.setEnabledNextPtrAtOnce(true);
        pfl_root.setLastUpdateTimeRelateObject(this);
        pfl_root.setPtrHandler(this);
        pfl_root.setKeepHeaderWhenRefresh(true);
        getMbUserInfoNo();
        appCouponlistmy();
        getMyIntegralInfoByApp();

        findViewById(R.id.yeLay).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(context, BalanceActivity.class).putExtra("ID_CARD", ID_CARD));
            }
        });
        findViewById(R.id.yhqLay).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(context, BranchCouponActivity.class).putExtra("branch_id", BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.HOTEL_ID)));
            }
        });
        findViewById(R.id.jfLay).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                launch(PointsDetailsActivity.class);
            }
        });
        findViewById(R.id.wyczLay).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ToastUtils.toastShort("暂未开放");
            }
        });
        findViewById(R.id.wddp).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ToastUtils.toastShort("暂未开放");
            }
        });

        findViewById(R.id.wdydd).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(context, BookingOrderActivity.class).putExtra("ID_CARD", ID_CARD));
            }
        });

        findViewById(R.id.allOrder).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(context, OrderActivity.class).putExtra("ID_CARD", ID_CARD)
                .putExtra("item", 0));
            }
        });
        findViewById(R.id.yqxOrder).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(context, OrderActivity.class).putExtra("ID_CARD", ID_CARD)
                        .putExtra("item", 1));
            }
        });
        findViewById(R.id.dzfOrder).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(context, OrderActivity.class).putExtra("ID_CARD", ID_CARD)
                        .putExtra("item", 2));
            }
        });
        findViewById(R.id.dclOrder).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(context, OrderActivity.class).putExtra("ID_CARD", ID_CARD)
                        .putExtra("item", 3));
            }
        });
        findViewById(R.id.ywcOrder).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(context, OrderActivity.class).putExtra("ID_CARD", ID_CARD)
                        .putExtra("item", 4));
            }
        });

        findViewById(R.id.yjfk).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                BookingActivity.choiceFragment(2);
        }
        });

        findViewById(R.id.wdxx).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                launch(PointsMallActivity.class);
            }
        });

        findViewById(R.id.myClick).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                launch(InformaticaNewActivity.class);
            }
        });

        findViewById(R.id.yqhy).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                BookingActivity.choiceFragment(0);
            }
        });

    }

    @Override
    public void fetchData() {

    }

    @Override
    public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
        return PtrDefaultHandler.checkContentCanBePulledDown(frame, content, header);
    }

    @Override
    public void onRefreshBegin(PtrFrameLayout frame) {
        getMbUserInfoNo();
        appCouponlistmy();
        getMyIntegralInfoByApp();
        pfl_root.refreshComplete();
    }

    private void getMyIntegralInfoByApp() {
        Call<ResultObjectModel> call = HttpAdapter.getSerives().getMyIntegralInfoByApp(BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.HOTEL_ID),BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.TOKEN));
        call.enqueue(new OnResponseListener<ResultObjectModel>(context) {
            @Override
            protected void onSuccess(ResultObjectModel userModelBaseResultModel) {
                if(userModelBaseResultModel.getResultCode().equals("0000")){
                    DecimalFormat decimalFormat = new DecimalFormat("###################.###########");
                    LinkedTreeMap linkedTreeMap = (LinkedTreeMap) userModelBaseResultModel.getData();
                    if(linkedTreeMap.containsKey("data")){
                        LinkedTreeMap dateLink = (LinkedTreeMap) linkedTreeMap.get("data");
                        if(dateLink.containsKey("AvailableIntegral")){
                            jfTxt.setText(decimalFormat.format(dateLink.get("AvailableIntegral")));
                        }
                    }

                }else if (userModelBaseResultModel.getResultCode().equals("1111")){
                    ToastUtils.toastShort("账号没有登录或在其它地方登录");
                    BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.TOKEN, "");
                    BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.IS_AUTO_LOGIN, "2");
                    PushManager.getInstance().unBindAlias(context, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.LOGIN_PHONE), false, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.CID));
                    Intent intent = new Intent(context, LoginActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                }
            }
        });
    }

    private void getMbUserInfoNo() {
        Call<ResultObjectModel> call = HttpAdapter.getSerives().getMbUserInfo(BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.TOKEN));
        call.enqueue(new OnResponseListener<ResultObjectModel>(context) {
            @Override
            protected void onSuccess(ResultObjectModel userModelBaseResultModel) {
                if(userModelBaseResultModel.getResultCode().equals("0000")){
                    DecimalFormat decimalFormat = new DecimalFormat("###################.###########");
                    LinkedTreeMap linkedTreeMap = (LinkedTreeMap) userModelBaseResultModel.getData();
                    name.setText(linkedTreeMap.get("NAME")+"");
                    ID_CARD = linkedTreeMap.get("ID_CARD")+"";

                    if (!(linkedTreeMap.get("IMG")+"").equals("null")){
                        Glide.with(context).load(HttpAdapter.HEAD_URL+linkedTreeMap.get("IMG")).placeholder(R.mipmap.mine_photo).error(R.mipmap.mine_photo).into(roundImg);
                    }else{
                        Glide.with(context).load("").placeholder(R.mipmap.mine_photo).error(R.mipmap.mine_photo).into(roundImg);
                    }
                    hotel_user_info();
                }else if (userModelBaseResultModel.getResultCode().equals("1111")){
                    ToastUtils.toastShort("账号没有登录或在其它地方登录");
                    BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.TOKEN, "");
                    BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.IS_AUTO_LOGIN, "2");
                    PushManager.getInstance().unBindAlias(context, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.LOGIN_PHONE), false, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.CID));
                    Intent intent = new Intent(context, LoginActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                }else{
                    ToastUtils.toastShort(userModelBaseResultModel.getResultMsg());
                }
            }
        });
    }

    private void hotel_user_info() {
        Call<ResultObjectModel> call = HttpAdapter.getSerives().userinfo(BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.HOTEL_ID), ID_CARD, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.TOKEN));
        call.enqueue(new OnNoResponseListener<ResultObjectModel>(context) {
            @Override
            protected void onSuccess(ResultObjectModel userModelBaseResultModel) {
                if(userModelBaseResultModel.getResultCode().equals("0000")){
                    DecimalFormat decimalFormat = new DecimalFormat("###################.###########");
                    LinkedTreeMap linkedTreeMap = (LinkedTreeMap) userModelBaseResultModel.getData();
                    if(linkedTreeMap.containsKey("code")){
                        if(decimalFormat.format(linkedTreeMap.get("code")).equals("200")){
                            LinkedTreeMap dataLink = (LinkedTreeMap) linkedTreeMap.get("data");
                            card.setText(dataLink.get("level")+"");
                            if(dataLink.containsKey("totalBalance")){
                                account_balance.setText("￥"+ decimalFormat.format(dataLink.get("totalBalance"))+"");
                            }

                        }
                    }
//                    card.setText(linkedTreeMap.get("lv_name")+"");
//                    account_balance.setText("￥"+linkedTreeMap.get("account_balance")+"");
                }
            }
        });
    }

    private void appCouponlistmy() {
        Call<ResultObjectModel> call = HttpAdapter.getSerives().appCouponlistmy("1",BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.TOKEN), "10", "1", BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.HOTEL_ID));
        call.enqueue(new OnResponseListener<ResultObjectModel>(context) {
            @Override
            protected void onSuccess(ResultObjectModel userModelBaseResultModel) {
                if(userModelBaseResultModel.getResultCode().equals("0000")){
                    DecimalFormat decimalFormat = new DecimalFormat("###################.###########");
                    LinkedTreeMap linkedTreeMap = (LinkedTreeMap) userModelBaseResultModel.getData();
                    LinkedTreeMap page = (LinkedTreeMap) linkedTreeMap.get("page");
                    yhqNum.setText(decimalFormat.format(page.get("totalCount")));

                }else if (userModelBaseResultModel.getResultCode().equals("1111")){
                    ToastUtils.toastShort("账号没有登录或在其它地方登录");
                    BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.TOKEN, "");
                    BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.IS_AUTO_LOGIN, "2");
                    Intent intent = new Intent(context, LoginActivity.class);
                    PushManager.getInstance().unBindAlias(context, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.LOGIN_PHONE), false, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.CID));
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                }else{
                    ToastUtils.toastShort(userModelBaseResultModel.getResultMsg());
                }
            }
        });
    }

}
